<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php 
$database = $_GET['database'];
$table = $_GET['table'];

$conn = mysql_connect("localhost", "root", "");
mysql_select_db($database);
$sql = "describe $table";
$result = mysql_query($sql);

//print_r($_GET);
//exit;
$bien_khoa_chinh = '';
$dk_khoa_chinh = '';
$bien_cot_ham_them = '';
$cot_ham_them = '';
$gia_tri_ham_them = '';
$bien_cot_ham_sua = '';
$gia_tri_ham_sua = '';
$dk_tim_kiem = '';


$bien_khoa_chinh_fm = '';
$bien_cot_ham_them_fm = '';
$bien_cot_ham_sua_fm = '';
$gia_tri_ham_them_goi_ham = '';
$gia_tri_ham_sua_goi_ham = '';

while ($row = mysql_fetch_assoc($result))
{
	
	
	if($row['Key'] == 'PRI')
	{
		//s.Ma_sua = '$masua'";
		$bien_khoa_chinh .= '$'.$row['Field'].', ';
		if( substr($row['Type'],0,3) == 'int' )
		{
			$bien_khoa_chinh_fm .= "\t$".$row['Field']. ' = intval($'.$row['Field'].');'."\n\t";
		}
		
		if( substr($row['Type'],0,7) == 'varchar' || $row['Type']  == 'text')
		{
			$bien_khoa_chinh_fm .=  "\t$".$row['Field'].  ' = addslashes($'.$row['Field'].');'."\n\t";
		}
		
		$dk_khoa_chinh .= $row['Field']. " = '$".$row['Field']. "', ";
		
	}
	else 
	{
		
		
		$bien_cot_ham_them .= '$'.$row['Field'].', ';
		
		$gia_tri_ham_them_goi_ham .= '$_POST[\''.$row['Field'].'\'],';
		
		if( substr($row['Type'],0,3) == 'int' )
		{
			$bien_cot_ham_them_fm .= "\t$".$row['Field']. ' = intval($'.$row['Field'].');'."\n\t";
		}
		
		if( substr($row['Type'],0,7) == 'varchar' || $row['Type']  == 'text')
		{
			$bien_cot_ham_them_fm .=  "\t$".$row['Field'].  ' = addslashes($'.$row['Field'].');'."\n\t";
		}
		
		if( substr($row['Type'],0,7) == 'varchar' || $row['Type']  == 'text')
		{
			$dk_tim_kiem .= "\t$".$row['Field'].' like %tu_khoa% or ';
		}	
		
		$cot_ham_them .= $row['Field'].',';
		
		$gia_tri_ham_them .= "'$".$row['Field']."',";
		
	
		$gia_tri_ham_sua .= $row['Field']. "= '$".$row['Field']."', ";
	}
	$bien_cot_ham_sua .= '$'.$row['Field'].', ';
	$gia_tri_ham_sua_goi_ham .= '$_POST[\''.$row['Field'].'\'],';
}

$bien_cot_ham_sua_fm = $bien_khoa_chinh_fm.$bien_cot_ham_them_fm;

 $bien_khoa_chinh=trim($bien_khoa_chinh, ', ');
 $dk_khoa_chinh=trim($dk_khoa_chinh, ', ');
 
 
 $bien_cot_ham_them=trim($bien_cot_ham_them, ', ');
 $cot_ham_them=trim($cot_ham_them, ', ');
 $gia_tri_ham_them=trim($gia_tri_ham_them, ', ');
 
 $bien_cot_ham_sua=trim($bien_cot_ham_sua, ', ');
 $gia_tri_ham_sua=trim($gia_tri_ham_sua, ', ');
 $bien_khoa_chinh_fm = trim($bien_khoa_chinh_fm,"\t");
 $bien_cot_ham_them_fm = trim($bien_cot_ham_them_fm,"\t");
 $bien_cot_ham_sua_fm = trim($bien_cot_ham_sua_fm,"\t");
 
 $gia_tri_ham_them_goi_ham = trim($gia_tri_ham_them_goi_ham, ',');
 $gia_tri_ham_sua_goi_ham = trim($gia_tri_ham_sua_goi_ham, ',');
 $dk_tim_kiem = trim($dk_tim_kiem, ' or ');


$str_danh_sach = 'function danh_sach($vi_tri, $so_luong)
	{
		$vi_tri = intval($vi_tri);
		$so_luong = intval($so_luong);
		$sql = "select * from '.$table.' limit $vi_tri, $so_luong";
		$this->setQuery($sql);
		return $this->loadAllRow();
	}';
$str_danh_sach_goi_ham = '$dt_xl_'.$table.'->danh_sach($vi_tri, $so_luong);';	

$str_so_luong = 'function so_luong()
	{
		$sql = "select count(*) from '.$table.'";
		$this->setQuery($sql);
		return $this->loadResult();
	}';
$str_so_luong_goi_ham = '$dt_xl_'.$table.'->so_luong();';

$str_xem = 'function xem('.$bien_khoa_chinh.')
	{
		'.$bien_khoa_chinh_fm.'
		$sql = "select * from '.$table.' where '.$dk_khoa_chinh.'";
		$this->setQuery($sql);
		return $this->loadRow();
	}';
$str_xem_goi_ham = '$dt_xl_'.$table.'->xem($_GET[\'$ma\']);';


$str_xoa = 'function xoa('.$bien_khoa_chinh.')
	{
		'.$bien_khoa_chinh_fm.'
		$sql = "delete from '.$table.' where '.$dk_khoa_chinh.'";
		$this->setQuery($sql);
		return $this->query();
	}';
$str_xoa_goi_ham = '$dt_xl_'.$table.'->xoa($_GET[\'$ma\']);';

	
$str_them = 'function them('.$bien_cot_ham_them.')
	{
		'.$bien_cot_ham_them_fm.'
		$sql = "INSERT INTO '.$table.'('.$cot_ham_them.') VALUES ('.$gia_tri_ham_them.')";
		$this->setQuery($sql);
		return $this->query();
	}';
$str_them_goi_ham = '$dt_xl_'.$table.'->them('.$gia_tri_ham_them_goi_ham.');';

$str_cap_nhat = 'function cap_nhat('.$bien_cot_ham_sua.')
	{
		'.$bien_cot_ham_sua_fm.'
		$sql ="UPDATE '.$table.' SET '.$gia_tri_ham_sua.' WHERE '.$dk_khoa_chinh.'";
		$this->setQuery($sql);
		return $this->query();
	}';
$str_cap_nhat_goi_ham = '$dt_xl_'.$table.'->cap_nhat('.$gia_tri_ham_sua_goi_ham.');'; 


$str_tim_kiem = 'function tim_kiem ($tu_khoa)
	{
		$sql = "select * from '.$table. ' where'.$dk_tim_kiem.'"; 
		$this->setQuery($sql);
		return $this->loadAllRow();	
	}';
$str_tim_kiem_goi_ham = '$dt_xl_'.$table.'->tim_kiem($tu_khoa);';


$str = '<?php
require_once("database.php");
class xl_'.$table.' extends database
{'."\n";

$str_goi_ham = '<?php'."\n";


if(!empty($_GET['danh_sach']))
{
	$str .= "\t".$str_danh_sach."\n";
	$str_goi_ham .= $str_danh_sach_goi_ham."\n";
}
if(!empty($_GET['so_luong']))
{
	$str .= "\t".$str_so_luong."\n";
	$str_goi_ham .= $str_so_luong_goi_ham."\n";
}
if(!empty($_GET['xem']))
{
	$str .= "\t".$str_xem."\n";
	$str_goi_ham .= $str_xem_goi_ham."\n";
}
	
if(!empty($_GET['xoa']))
{
	$str .= "\t".$str_xoa."\n";
	$str_goi_ham .= $str_xoa_goi_ham."\n";
}
	
if(!empty($_GET['them']))
{
	$str .= "\t".$str_them."\n";
	$str_goi_ham .= $str_them_goi_ham."\n";
}
	
if(!empty($_GET['cap_nhat']))
{
	$str .= "\t".$str_cap_nhat."\n";
	$str_goi_ham .= $str_cap_nhat_goi_ham."\n";
}

if(!empty($_GET['tim_kiem']))
{
	$str .="\t".$str_tim_kiem."\n";
	$str_goi_ham .= $str_tim_kiem_goi_ham."\n";
}

$str.= " }\n?>";	
$str_goi_ham .='?>';
	
$f = fopen("data/xl_".$table.".php", "w");
fwrite($f, $str);
fclose($f);


$f_goi_ham = fopen("data/xl_".$table."_goi_ham.php", "w");
fwrite($f_goi_ham, $str_goi_ham);
fclose($f_goi_ham);


echo "phát sinh thành công";
?>
<input type="button" value="Back" onclick="window.location.href='index.php'"/>
</body>
</html>